package com.example.backend.service.impl;



import com.example.backend.mapper.UserMapper;
import com.example.backend.pojo.User;
import com.example.backend.service.UserService;
import com.example.backend.util.Md5Util;
import com.example.backend.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import static java.time.LocalDate.now;

@Service
public class UserServiceimpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserByName(String username) {
        User u=userMapper.getUserByName(username);
        return u;
    }

    @Override
    public void register(String username, String password) {
        String md5String = Md5Util.getMD5String(password);
        userMapper.add(username,md5String,"https://hiram.oss-cn-chengdu.aliyuncs.com/default.png");
    }

    @Override
    public void update(User user) {
        user.setUpdatetime(LocalDateTime.now());
        Integer age = user.getAge();
        Integer gender = user.getGender();
        Integer i;
        if(age>=60){
            i=7;
        }else{
             i=age/10+1;
        }
        if(gender==1){
            i=i+7;
        }
        user.setUsertypeid(i);
        userMapper.update(user);
    }

    @Override
    public void updateAvatar(String url) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updateAvatar(url,id);
    }

    @Override
    public void updatePwd(Integer id, String new_pwd) {
        userMapper.updatePwd(id,new_pwd);
    }

    @Override
    public void updatevip(Integer month) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updatevip(LocalDateTime.now().plusMonths(month),id);
    }

    @Override
    public void delete(Integer id) {

        userMapper.delete(id);
    }

    @Override
    public void relive(Integer id) {

        userMapper.relive(id);
    }

    @Override
    public List<User> getall() {
        return userMapper.getall();
    }

    @Override
    public void updatemoney(Double money, Integer userid) {
        userMapper.updatemoney(money,userid);
    }
    @Override
    public Double getmoney(Integer userid) {
        return userMapper.getmoney(userid);
    }
}
